package com.photoeditor.demo.ui.adapter.home;

import android.graphics.drawable.TransitionDrawable;
import android.support.constraint.ConstraintLayout;
import android.support.v4.view.PagerAdapter;
import android.text.TextPaint;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.ImageView;
import android.widget.TextView;

import com.common.base.utils.DeviceUtils;
import com.photoeditor.R;

public class UltraPagerAdapter extends PagerAdapter {

    private int MAX_TEXT_WIDTH = (int) (DeviceUtils.getScreenWidthPx() * 0.43);

    private boolean isMultiScr;
    private boolean isNeedTry;
    private TransitionDrawable transitionDrawable1 = null;
    private TransitionDrawable transitionDrawable2 = null;
    private TransitionDrawable transitionDrawable3 = null;
    private int posCount = 0;

    public UltraPagerAdapter(boolean isMultiScr, boolean isNeedTry) {
        this.isMultiScr = isMultiScr;
        this.isNeedTry = isNeedTry;
    }

    @Override
    public int getCount() {
        return 3;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, final int position) {
        ConstraintLayout linearLayout = (ConstraintLayout) LayoutInflater.from(container.getContext()).inflate(R.layout.layout_child, null);
        final TextView tvName = linearLayout.findViewById(R.id.tvName);
        final TextView tvName2 = linearLayout.findViewById(R.id.tvName2);
        View tvTry = linearLayout.findViewById(R.id.tvTry);
        if (isNeedTry) {
            tvTry.setVisibility(View.VISIBLE);
            tvTry.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    itemViewClick.tryClick(position);
                }
            });
            ConstraintLayout.LayoutParams layoutParams = (ConstraintLayout.LayoutParams) tvName.getLayoutParams();
            layoutParams.topMargin = DeviceUtils.dip2px(43);
            tvName.setLayoutParams(layoutParams);
            tvName.setTextSize(38);
//            tvName.setLineSpacing(0, 1);
        } else {
            tvTry.setVisibility(View.GONE);
            ConstraintLayout.LayoutParams layoutParams = (ConstraintLayout.LayoutParams) tvName.getLayoutParams();
            layoutParams.topMargin = DeviceUtils.dip2px(106);
            tvName.setLayoutParams(layoutParams);
            tvName.setTextSize(38);
//            tvName.setLineSpacing(56, 1);
        }
        ImageView ivFilter = linearLayout.findViewById(R.id.ivFilter);
        ImageView ivAging = linearLayout.findViewById(R.id.ivFaceAging);
        ImageView ivGender = linearLayout.findViewById(R.id.ivGender);
//        linearLayout.setId(R.id.item_id);
        switch (position) {
            case 0://换性
                if (posCount >= 3) {
                    transitionDrawable1.reverseTransition(1500);
                }

                ivGender.setImageResource(R.drawable.transition_drawable_gender);
                transitionDrawable3 = (TransitionDrawable) ivGender.getDrawable();
                if (posCount == 0)
                    transitionDrawable3.startTransition(1500);

                tvName.setText(container.getContext().getResources().getString(R.string.top_banner_gender_swap));
                tvName2.setText(container.getContext().getResources().getString(R.string.top_banner_gender_swap2));
                break;


            case 1://老换
                //page0 恢复
                if (posCount >= 3) {
                    transitionDrawable3.reverseTransition(1500);
                }

                ivAging.setImageResource(R.drawable.transition_drawable_time_machine);
                transitionDrawable2 = (TransitionDrawable) ivAging.getDrawable();
                if (posCount == 1)
                    transitionDrawable2.startTransition(1500);

                tvName.setText(container.getContext().getResources().getString(R.string.top_banner_face_aging));
                tvName2.setText(container.getContext().getResources().getString(R.string.top_banner_face_aging2));
                break;

            case 2://卡通滤镜
                if (posCount >= 3) {
                    transitionDrawable2.reverseTransition(1500);
                }
                //page0 渐变
                ivFilter.setImageResource(R.drawable.transition_drawable_filter);
                transitionDrawable1 = (TransitionDrawable) ivFilter.getDrawable();
                if (posCount == 2)
                    transitionDrawable1.startTransition(1500);

                tvName.setText(container.getContext().getResources().getString(R.string.top_banner_artistic_filters));
                tvName2.setText(container.getContext().getResources().getString(R.string.top_banner_artistic_filters2));
                break;


        }
        final ViewTreeObserver viewTreeObserver = container.getViewTreeObserver();
        viewTreeObserver.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            @Override
            public boolean onPreDraw() {
                if (viewTreeObserver != null && viewTreeObserver.isAlive()) {
                    viewTreeObserver.removeOnPreDrawListener(this);
                }
                if (tvName == null) {
                    return false;
                }

                measureTextViewSize(tvName);

                return false;
            }
        });
        measureTextViewSize(tvName2);

        posCount++;
        container.addView(linearLayout);
        return linearLayout;
    }

    private void measureTextViewSize(TextView textView) {
        if (!canShowSpecifiesLengthText(textView)) {
            int measuredWidth = MAX_TEXT_WIDTH;
            float textSize = textView.getTextSize();
            final float valueSp2 = DeviceUtils.sp2pxF(2);
            final float valueMinSp = DeviceUtils.sp2pxF(10);
            TextPaint textPaint = textView.getPaint();
            String text = textView.getText().toString();
            int textViewWidth = (int) textPaint.measureText(text);
            do {
                textSize -= valueSp2;
                if (textSize < valueMinSp) {
                    break;
                } else {
                    textPaint.setTextSize(textSize);
                    textViewWidth = (int) textPaint.measureText(text);
                }
            } while ((1.0 * textViewWidth / measuredWidth) > 1);
            textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
        }
    }

    private boolean canShowSpecifiesLengthText(TextView textView) {
        int maxLines = textView.getMaxLines();
        int measuredWidth = MAX_TEXT_WIDTH;
        if (measuredWidth == 0) {
            return false;
        }
        TextPaint textPaint = textView.getPaint();
        String text = textView.getText().toString();
        int textViewWidth = (int) textPaint.measureText(text);
        if (1.0 * textViewWidth / measuredWidth > maxLines) {
            return false;
        }
        return true;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        ConstraintLayout view = (ConstraintLayout) object;
        container.removeView(view);
    }


    //添加子view的点击事件回调
    public interface ItemViewClick {
        void tryClick(int pos);
    }

    private ItemViewClick itemViewClick;

    public void setItemViewClick(ItemViewClick itemViewClick) {
        this.itemViewClick = itemViewClick;
    }
}
